/*
 * XmlHandler Java Class
 * 22.03.2009
 */
package com.meraklisina.latexeditor;

import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author kursattopcu
 */
public class XmlHandler {

    Document xmlDoc;
    public static DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
    public static DocumentBuilder builder = null;

    public XmlHandler(String xmlFilePath) {
        this.xmlDoc = ParseXmlDocument(xmlFilePath);
    }

    public static Document ParseXmlDocument(String filepath) {
        try {
            File file = new File(filepath);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            return doc;
        } catch (Exception ex) {
            String exceptionDetail = ExceptionHandler.ProcessException(ex);
            return null;
        }
    }

    public NodeList GetElementsByTagName(String tagName) {
        return this.xmlDoc.getElementsByTagName(tagName);
    }

    public Element GetElementById(String id) {
        return this.xmlDoc.getElementById(id);
    }

    public String GetChildNodeValue(Node mainNode, String tagName) {
        Element mainElement = (Element) mainNode;

        NodeList childList = mainElement.getElementsByTagName(tagName);
        if (childList == null) {
            return null;
        }

        Element childElement = (Element) childList.item(0);
        if (childElement == null) {
            return null;
        }

        NodeList child = childElement.getChildNodes();
        if (child == null) {
            return null;
        }

        Node childNode = (Node) child.item(0);
        if (childNode == null) {
            return null;
        }

        return childNode.getNodeValue();
    }
    /**
     * Transform DOM Document to xml String.
     * @param document DOM Document that will streamed to String
     * @return String that represent DOM document's xml.
     */
    public static String createXmlString(Document document) {
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();

            transformer.setParameter(javax.xml.transform.OutputKeys.INDENT,
                    "yes");
            transformer.setParameter(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION,
                    "yes");
            DOMSource source = new DOMSource(document);
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            transformer.transform(source, result);
            String xmlString = sw.toString();
            return xmlString;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * Creates an empty DOM document
     * @return Empty document
     */
    public static Document createXmlDocument() {
        Document document = null;
        try {
            if (builder == null) {
                builder = factory.newDocumentBuilder();
            }
            document = builder.newDocument();
        } catch (Exception e) {
            System.out.println("ERROR");
            return null;
        }
        return document;
    }
}
